<?php

class ActionEditUser extends Action {
	public function IsPermitted(){ return User::GetCurrent()->IsAdministrator(); }
	public function GetTitle(){ return 'Modifier utilisateur'; }
	public function GetIconName(){ return 'oxy/ico/User'; }
	public function GetParentAction(){ return new ActionBrowseUsers(); }

	private $user;
	public function __construct(User $user){ parent::__construct(); $this->user = $user; }
	public function GetUrlArgs(){ return array('idUser'=>$this->user); }
	public static function Make($class){ return new $class(User::Retrieve(Http::$GET['idUser']->AsID())); }


	public function Render(){

		$ui = $this->user->GetUIObject();

		$v = new ValidatorSet();
		if ($this->IsPostback()){

			if ( Http::$POST['Delete']->AsString() != null ) {
				if ( User::GetCurrent()->id->AsInt()==$this->user->id->AsInt())
					throw new ApplicationException( Lemma::ErrorCannotDeleteCurrentUser() );
				$this->user->Delete();
				Oxygen::Redirect($this->GetParentAction());
				return;
			}

			$ui->Read(Http::$POST);

			$xx = User::RetrieveByUsername($this->user->Username);

			$v->Username->CheckMandatory($this->user->Username);
			$v->Username->Check($xx==null || $xx->id->AsInt()==$this->user->id->AsInt(), Lemma::ErrorUsernameExists());

			if ($v->HasPassed()){
				$this->user->Save();
				Oxygen::Redirect($this->GetParentAction());
				return;
			}
		}


		echo '<br/>';
		echo $this->GetForm();
		$z = FieldTableControl::Make()->WithWidth(500);
		$z->Add(TextboxControl::Make($ui->Username))->WithValidator($v->Username);
		$z->Add(TextboxControl::Make($ui->Email));
		$z->Add(ButtonControl::Make()->WithIsSubmit(true)->WithValue(Lemma::OK()));
		$z->Write(' &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; ');
		$z->Write(' &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; ');
		$z->Write(' &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; ');
		$z->Write(' &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; ');
		$z->Write(' &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  ');
		$z->Write(ButtonControl::Make('Delete')->WithIsSubmit(true)->WithValue(Lemma::Delete()));
		$z->Render();
		echo $this->EndForm();





	}
}

?>
